//
//  ZMDataBaseManager.swift
//  LoveBeen
//
//  Created by pg on 2017/8/10.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit
import FMDB

private let DBPath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString).appendingPathComponent("data.db")

class ZMDataBaseManager: NSObject {

    //数据库操作队列
    var queue: FMDatabaseQueue!
    //单例全局访问点
    static let sharedManager: ZMDataBaseManager = ZMDataBaseManager()
    
    //构造函数
   private override init() {
        super.init()
        //输出数据库地址
        print(DBPath)
       //创建数据库
        queue = FMDatabaseQueue(path: DBPath)
    
    }
    //MARK:- 创建表格
    func creatTabel(sqlStr:String) -> Bool {
       var result: Bool = false
        queue.inDatabase { (db) in
           result = db.executeStatements(sqlStr)
        }
        return result
    }
    
    
  
    
}

extension ZMDataBaseManager{
    func creatProductsTable() -> Bool {
        
       // let sqlStr =
        
        
      return false
    }
}




//创建表格相关
extension ZMDataBaseManager{
    
    //MARK:- 根据模型获取创建表格的sql语句
     func getCreatTableSqlString(modelClass:AnyClass)->String{
        //获取模型类中的属性名称和类型
        let ivarlist = ZMRunTime.shared().zm_getIvarList(modelClass) as! [[String:Any]]
        let tableName = NSStringFromClass(modelClass)
        var ivarStr: String = ""
        for dic in ivarlist{
         //   ivarStr.append(dic["ivarName"] ?? "")
         //   ivarStr.append(dic["type"] ?? "")
        }
        
        let tempstr =  (ivarStr as NSString).substring(to: (ivarStr as NSString).length-1)
        let sqlStr = "create table if not exists \(tableName) (id integre not NUll primary key autoincrement,\(tempstr))"
        
        return sqlStr
    }
    
}




















